﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;

namespace UI.ModTrain
{
    public partial class TeacherAdd : System.Web.UI.Page
    {
        DefDicBiz defDicBiz = new DefDicBiz();
        TeacherBiz teacherBiz = new TeacherBiz();
        string teacherCode = "";
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["TeacherCode"] != null)
            {
                teacherCode = Request.Params["TeacherCode"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                JSUtility.AddConfirm(btnDelete, "您真的要删除该员工的信息吗？");
                txtJoinTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (teacherCode != "")
                {
                    btnAdd.Enabled = false;
                    btnDelete.Enabled = true;
                    btnUpdate.Enabled = true;
                    BindData(teacherCode);
                }
            }
        }
        private void BindData(string teacherCode)
        {
            DataTable dt = teacherBiz.GetOneInfo(teacherCode);
            if (dt.Rows.Count > 0)
            {
                txtTeacherCode.Text = teacherCode;
                txtTeacherCode.ReadOnly = true;
                txtEnName.Text = dt.Rows[0]["EnName"].ToString();
                txtCnName.Text = dt.Rows[0]["CnName"].ToString();
                txtTitle.Text = dt.Rows[0]["Title"].ToString();
                txtLinkWay.Text = dt.Rows[0]["LinkWay"].ToString();
                defDicBiz.InitDropDownList(ddlSex, dt.Rows[0]["Sex"].ToString().Trim());
                if (dt.Rows[0]["EnName"].ToString() == "Y") cbIsInner.Checked = true;
                else
                    cbIsInner.Checked = false;
                string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                if (Utils.RegexRule.IsDate(JoinTime))
                    txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");

                txtAddr.Text = dt.Rows[0]["Addr"].ToString();
                txtFromCom.Text = dt.Rows[0]["FromCom"].ToString();
                txtProfile.Text = dt.Rows[0]["Profile"].ToString();
                txtTrainField.Text = dt.Rows[0]["TrainField"].ToString();
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
            }

        }
        
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "TeacherAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "TeacherList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "您没有增加的权限！");
                    return;
                }
            }
            string code = txtTeacherCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(code))
            {
                JSUtility.Alert(this, "编号必须填写");
                return;
            }
            if (teacherBiz.IsExistTeacherCode(code))
            {
                JSUtility.Alert(this, "编号不能重复");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("TeacherCode", code);
            sdList.Add("CnName", txtCnName.Text.Trim());
            sdList.Add("EnName", txtEnName.Text.Trim());
            sdList.Add("Sex", ddlSex.SelectedValue);
            sdList.Add("Title", txtTitle.Text.Trim());
            sdList.Add("JoinTime", txtJoinTime.Text.Trim());
            sdList.Add("IsInner", (cbIsInner.Checked==true)?"Y":"N");
            sdList.Add("LinkWay", txtLinkWay.Text.Trim());
            sdList.Add("Addr", txtAddr.Text);

            sdList.Add("FromCom", txtFromCom.Text.Trim());
            sdList.Add("TrainField", txtTrainField.Text.Trim());
            sdList.Add("Profile", txtProfile.Text.Trim());
            sdList.Add("Memo", txtMemo.Text.Trim());
            string info = "";
            if (teacherBiz.Insert(sdList))
                info = "增加信息成功";
            else
                info = "增加信息失败";
            JSUtility.Alert(this, info);
        }   

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "TeacherAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "TeacherList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "您没有更新的权限！");
                    return;
                }
            }
            string code = txtTeacherCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(code))
            {
                JSUtility.Alert(this, "编号必须填写");
                return;
            }
            if (code != teacherCode)
            {
                JSUtility.Alert(this, "不要手工填写编号");
                return;
            }
            if (!teacherBiz.IsExistTeacherCode(code))
            {
                JSUtility.Alert(this, "编号还没有被使用");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("TeacherCode", code);
            sdList.Add("CnName", txtCnName.Text.Trim());
            sdList.Add("EnName", txtEnName.Text.Trim());
            sdList.Add("Sex", ddlSex.SelectedValue);
            sdList.Add("Title", txtTitle.Text.Trim());
            sdList.Add("JoinTime", txtJoinTime.Text.Trim());
            sdList.Add("IsInner", (cbIsInner.Checked == true) ? "Y" : "N");
            sdList.Add("LinkWay", txtLinkWay.Text.Trim());
            sdList.Add("Addr", txtAddr.Text);

            sdList.Add("FromCom", txtFromCom.Text.Trim());
            sdList.Add("TrainField", txtTrainField.Text.Trim());
            sdList.Add("Profile", txtProfile.Text.Trim());
            sdList.Add("Memo", txtMemo.Text.Trim());
            string info = "";
            if (teacherBiz.Update(sdList))
                info = "更新信息成功";
            else
                info = "更新信息失败";
            JSUtility.Alert(this, info);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("TeacherAdd.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "TeacherAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "TeacherList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "您没有删除的权限！");
                    return;
                }
            }
            if (!string.IsNullOrWhiteSpace(teacherCode))
            {
                if (teacherBiz.DeleteOneInfo(teacherCode))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "没有选择信息");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }
    }
}